package com.shuqi.model.net;

import java.util.Properties;

import android.content.Context;

import com.shuqi.common.utils.Log4an;
import com.shuqi.model.sharedprefs.ServerDomains;


/**
 * @des 描述 域名集合类
 */
public class UrlManager {
    private final static String logTag = "UrlManager";


    /** 书旗 [读]域名 */
    public final static String DOMAIN_READ = "api.shuqireader.com";
    /** 书旗 [读]域名序号 */
    public static final int COLUMN_DOMAIN_READ = 0;

    /** 书旗 [写]域名 */
    public final static String DOMAIN_WRITE = "api2.shuqireader.com";
    /** 书旗 [写]域名序号 */
    public static final int COLUMN_DOMAIN_WRITE = 1;

    /** pp账户 域名 */
    public final static String DOMAIN_ACCOUNT = "account.shuqireader.com";
    /** pp账户 域名序号 */
    public static final int COLUMN_DOMAIN_ACCOUNT = 2;

    /** 搜索 域名 */
    public final static String DOMAIN_SEARCH = "api3.shuqireader.com";
    /** 搜索 域名序号 */
    public static final int COLUMN_DOMAIN_SEARCH = 3;

    /** 支付域名 */
    public final static String DOMAIN_PAY = "pay.shuqireader.com";
    /** 支付 域名序号 */
    public static final int COLUMN_DOMAIN_PAY = 4;

    /** appinfo 域名 */
    public final static String DOMAIN_APPINFO = "api7.shuqireader.com";
    /** appinfo 域名序号 */
    public static final int COLUMN_DOMAIN_APPINFO = 5;

    
    /** 书旗demo域名:读域名*/
    public final static String DEMO_DOMAIN_READ = "demo.shuqi.com";
    /** 书旗demo域名:写域名*/
    public final static String DEMO_DOMAIN_WRITE = "demo.shuqi.com";
    /** 账户demo域名 */
    public final static String DEMO_DOMAIN_ACCOUNT = "ppdemo.3gpp.cn";
    /** 搜索demo域名 */
    public final static String DEMO_DOMAIN_SEARCH = "apiso.alidemo.3gpp.cn";
    /** 支付demo域名 */
    public final static String DEMO_DOMAIN_PAY = "ppdemo.3gpp.cn";
    /** appInfo的demo域名 */
    public final static String DEMO_DOMAIN_APPINFO = "ppdemo.3gpp.cn";

    //----------网页域名-----------
    
    /**网页版书城的线上域名*/
    public final static String DOMAIN_WEB_BOOKSTORE = "bookstore.shuqireader.com";

    
    /**神马网页的DEMO域名*/
    public final static String DEMO_DOMAIN_WEB_SHENMA = "soutouch.smdemo.3gpp.net.cn";
    /**网页版书城的DEMO域名*/
    public final static String DEMO_DOMAIN_WEB_BOOKSTORE = "bs.demowx.shuqi.com";
    
    /**域名DEMO/ONLINE切换控制开关：true 为线上*/
    public static boolean[] onlineSwitchs;
    /**域名DEMO/ONLINE切换控制开关:demo域名*/
    public static final String SWITCH_DEMO = "0";
    public static final String SWITCH_ONLINE = "1";
    
    /**一搜DEMO/ONLINE切换控制开关：true 为线上*/
    public static boolean[] onlineSwitchs_yisou;
    /**网页版书城DEMO/ONLINE切换控制开关：true 为线上*/
    public static boolean[] onlineSwitchs_shuqi;

    /**
     * 网页一搜是否使用线上域名
     * @param context
     * @return
     */
    public static boolean isWebYisouDemoDomain(Context context){
        if(onlineSwitchs_yisou == null){
            onlineSwitchs_yisou = new boolean[]{true};
            try {
                Properties pro = new Properties();
                pro.load(context.getAssets().open("config/domain.ini"));
                onlineSwitchs_yisou[0] = !SWITCH_DEMO.equals(pro.getProperty("domain_webyisou"));
            } catch (Exception e) {
                Log4an.e(logTag, "getProperty error:"+e.toString());
                onlineSwitchs_yisou[0] = true;
            }
        }
        return !onlineSwitchs_yisou[0];//取反
    }
    /**
     * 网页版书城是否使用线上域名
     * @param context
     * @return
     */
    public static boolean isWebShuqiDemoDomain(Context context){
        if(onlineSwitchs_shuqi == null){
            onlineSwitchs_shuqi = new boolean[]{true};
            try {
                Properties pro = new Properties();
                pro.load(context.getAssets().open("config/domain.ini"));
                onlineSwitchs_shuqi[0] = !SWITCH_DEMO.equals(pro.getProperty("domain_webshuqi"));
            } catch (Exception e) {
                Log4an.e(logTag, "getProperty error:"+e.toString());
                onlineSwitchs_shuqi[0] = true;
            }
        }
        return !onlineSwitchs_shuqi[0];//取反
    }
            
    
    /***
     * 查看当前是否为demo域名
     * @param context
     * @param column
     * @return
     */
    private static boolean isOnlineDomain(Context context, int column){
        if(onlineSwitchs==null){
            onlineSwitchs = new boolean[6];
            try {
                Properties pro = new Properties();
                pro.load(context.getAssets().open("config/domain.ini"));
                onlineSwitchs[0] = !SWITCH_DEMO.equals(pro.getProperty("domain_read"));
                onlineSwitchs[1] = !SWITCH_DEMO.equals(pro.getProperty("domain_write"));
                onlineSwitchs[2] = !SWITCH_DEMO.equals(pro.getProperty("domain_account"));
                onlineSwitchs[3] = !SWITCH_DEMO.equals(pro.getProperty("domain_search"));
                onlineSwitchs[4] = !SWITCH_DEMO.equals(pro.getProperty("domain_pay"));
                onlineSwitchs[5] = !SWITCH_DEMO.equals(pro.getProperty("domain_app"));
            } catch (Exception e) {
                Log4an.e(logTag, "getProperty error:"+e.toString());
                for (int i = 0; i < onlineSwitchs.length; i++) {
                    onlineSwitchs[i] = true;
                }
            }
            Log4an.i(logTag, "domain_read online："+onlineSwitchs[0]);
            Log4an.i(logTag, "domain_write online："+onlineSwitchs[1]);
            Log4an.i(logTag, "domain_account online："+onlineSwitchs[2]);
            Log4an.i(logTag, "domain_search online："+onlineSwitchs[3]);
            Log4an.i(logTag, "domain_pay online："+onlineSwitchs[4]);
            Log4an.i(logTag, "domain_app online："+onlineSwitchs[5]);
        }
        if(column>=0 && column<=5 && column < onlineSwitchs.length){
            return onlineSwitchs[column];
        }else{
            Log4an.e(logTag, "error column:"+column);
            return true;
        }
    }
    
    /**
     * @描述 获取第column列的动态域名,使用那组由用户选择备选服务器决定,默认第0组;
     * @param param
     * @return bare_field_name
     */
    public static String[] getUrls(Context context, int column) {
        if (column < 0 || column > 5) {
            throw new IllegalArgumentException("column 非法:"+column);
        }
        String[] urls = new String[3];
        if(isOnlineDomain(context, column)){
            changeOnline(context, urls, column);
        }else{
            Log4an.e(logTag, "change demo domain: "+column);
            if(column == COLUMN_DOMAIN_READ){
                changeDemo(urls, DEMO_DOMAIN_READ);
                
            }else if(column == COLUMN_DOMAIN_WRITE){
                changeDemo(urls, DEMO_DOMAIN_WRITE);
                
            }else if(column == COLUMN_DOMAIN_ACCOUNT){
                changeDemo(urls, DEMO_DOMAIN_ACCOUNT);
                
            }else if(column == COLUMN_DOMAIN_SEARCH){
                changeDemo(urls, DEMO_DOMAIN_SEARCH);
                
            }else if(column == COLUMN_DOMAIN_PAY){
                changeDemo(urls, DEMO_DOMAIN_PAY);
                
            }else if(column == COLUMN_DOMAIN_APPINFO){
                changeDemo(urls, DEMO_DOMAIN_APPINFO);
            }
        }
        for (int i = 0; i < urls.length; i++) {
            Log4an.i(logTag, urls[i]);
        }
        return urls;
    }
    /***
     * 数组全部切换到demo域名
     * @param urls <B>不能为空</B>
     * @param demoDomain <B>不能为空</B>
     */
    private static void changeDemo(String[] urls,String demoDomain){
        for (int i = 0; i < urls.length; i++) {
            urls[i] = demoDomain;
        }
    }
    /**
     * 动态使用线上域名
     * @param context
     * @param urls 重试的数组
     * @param column 域名列
     */
    private static void changeOnline(Context context,String[] urls,int column){
        ServerDomains.getInstance().getDomainsByType(context, urls, column);
    } 
    
    
    /** 默认备选、重试服务器列表 */
    public final static String[][][] SERVER_IPS = {
            {
                    {"api.shuqireader.com", "api2.shuqireader.com", "account.shuqireader.com",
                            "api3.shuqireader.com", "pay.shuqireader.com", "api7.shuqireader.com"}, // 第一组默认IP
                    {"111.13.45.181", "59.151.47.15", "59.151.60.110", "42.120.172.14",
                            "59.151.60.110", "api7.shuqireader.com"}, // 第一组重试1
                    {"210.14.131.51", "api2.11222.cn", "59.151.60.105", "api3.11222.cn",
                            "59.151.60.105", "api7.shuqireader.com"}// 第一组重试2
            },
            {
                    {"api4.shuqireader.com", "api2.shuqireader.com", "account.shuqireader.com",
                            "api3.shuqireader.com", "pay.shuqireader.com", "api7.shuqireader.com"},// 第二组默认IP
                    {"210.14.131.51", "59.151.47.15", "59.151.60.110", "42.120.172.14",
                            "59.151.60.110", "api7.shuqireader.com"}, // 第二组重试1
                    {"111.13.45.181", "api2.11222.cn", "59.151.60.105", "api3.11222.cn",
                            "59.151.60.105", "api7.shuqireader.com"} // 第二组重试2
            },
            {
                    {"api.11222.cn", "api2.11222.cn", "account.shuqireader.com", "api3.11222.cn",
                            "pay.shuqireader.com", "api7.shuqireader.com"}, // 第三组默认IP
                    {"111.13.45.181", "59.151.47.15", "59.151.60.110", "42.120.172.14",
                            "59.151.60.110", "api7.shuqireader.com"}, // 第三组重试1
                    {"210.14.131.51", "api2.shuqireader.com", "59.151.60.105",
                            "api3.shuqireader.com", "59.151.60.105", "api7.shuqireader.com"} // 第三组重试2
            }};
}
